/**
 * Created by Yu Jin Xian on 2016/11/25.
 */
var express = require('express');

var app = express();

var handlebars = require('express3-handlebars').create({defaultLayout: 'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

app.set('port', process.env.PORT || 3000);

app.use(express.static( __dirname + '/public'));

var fortunes = [
    'Conquer your fears or they will conquer you.',
    'Rivers need springs.',
    'Do not fear what you don\'t know.',
    'You will have a pleasant surprise.',
    'Whenever possible, keep it simple.'
];

app.get('/', function (req, res) {
    res.render('home');
});

app.get('/about', function (req, res) {
    var randowFortune = fortunes[Math.floor(Math.random() * fortunes.length)];
    res.render('about', {fortune: randowFortune});
});

// 定制 404 页面
app.use(function (req, res) {
    res.status(404);
    res.render('404');
});

// 定制 500 页面
app.use(function (err, req, res, next) {
    console.log(err.stack);
    res.status(500);
    res.render('500');
});

app.listen(app.get('port'), function () {
    console.log('Express stated on http://localhost:' + app.get('port') + '; press Ctrl+C to terminate.');
});

function plainText() {
    app.get('/', function (req, res) {
        res.type('text/plain');
        res.send('Meadowlark Travel');
    });

    app.get('/about', function (req, res) {
        res.type('text/plain');
        res.send('About Meadowlark Travel');
    });

    // 定制 404 页面
    app.use(function (req, res) {
        res.type('text/plain');
        res.status(404);
        res.send('404 - Not Found');
    });

// 定制 500 页面
    app.use(function (err, req, res, next) {
        console.log(err.stack);
        res.type('text/plain');
        res.status(500);
        res.send('500 - Server Found');
    });
}

function filterRouter() {
    app.get('/about/*', function (req, res) {
        // 发送内容...
    });

    app.get('/about/contact', function (req, res) {
        // 发送内容...
    });

    app.get('/about/directions', function (req, res) {
        // 发送内容...
    });
}

